package com.gugu.procedure.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class StringUtil extends org.apache.commons.lang3.StringUtils {
    private static final Pattern underlinePattern = Pattern.compile("_(\\w)");

    private static final Pattern camelPattern = Pattern.compile("[A-Z]");

    public static boolean equals(String str1, String str2) {
        str1 = trim(Optional.ofNullable(str1).orElse(""));
        str2 = trim(Optional.ofNullable(str2).orElse(""));
        return Objects.equals(str1, str2);
    }

    public static boolean notEquals(String str1, String str2) {
        return !equals(str1, str2);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.equals("");
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String getString(String[] str, String spliChar) {
        StringBuffer sb = new StringBuffer();
        if (str != null) {
            for (String s : str) {
                sb.append(s).append(spliChar);
            }
        }
        if (sb.toString().contains(spliChar)) {
            return sb.subSequence(0, sb.length() - 1).toString();
        }
        return "";
    }

    public static String getTransNo() {
        return getRandomChar(18);
    }


    public static String getCode4() {
        String time = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            String n = String.valueOf(random.nextInt(10));
            time = time + n;
        }
        return time;
    }


    public static String nullHandle(String str) {
        if (str == null) {
            return "";
        }
        return str;
    }

    public static String getRandomNumber() {
        String time = "";
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            String n = String.valueOf(random.nextInt(10));
            time = time + n;
        }
        return time;
    }

    /**
     * 去掉字符串两端的空白，null值返回""
     *
     * @param str
     * @return
     */
    public static String trim(String str) {
        return Optional.ofNullable(str).map(s -> s.trim()).orElse("");
    }

    /**
     * 校验是否为数字
     */
    public static boolean isNum(String num) {

        if (num == null || "".equals(num)) {
            return false;
        }

        if (num.startsWith("-")) {
            return isNum(num.substring(1));
        }

        Pattern pattern = Pattern.compile("^\\d+$");
        Matcher isNum = pattern.matcher(num);
        return isNum.matches();
    }

    /**
     * 校验是否为数字或者字母
     */
    public static boolean isNumOrChar(String num) {

        if (num == null || "".equals(num)) {
            return false;
        }

        if (num.startsWith("-")) {
            return isNum(num.substring(1));
        }

        Pattern pattern = Pattern.compile("^[A-Za-z0-9]+$");
        Matcher isNum = pattern.matcher(num);
        return isNum.matches();
    }

    /**
     * 一对多比较 values notNUll
     *
     * @param str1
     * @param values
     * @return
     */
    public static boolean equalsAny(String str1, String... values) {
        final String str = trim(Optional.ofNullable(str1).orElse(""));
        return Arrays.asList(values).stream()
                .anyMatch(value -> Objects.equals(str, trim(Optional.ofNullable(value).orElse(""))));
    }


    /**
     * 字符串分割成集合
     *
     * @param str
     * @param separator
     * @return
     */
    public static Set<String> stringToSet(String str, String separator) {
        if (null == str || null == separator || str.isEmpty()) {
            return Collections.emptySet();
        }
        return Arrays.stream(str.split(separator)).collect(Collectors.toSet());
    }

    //  获取小数点指定长度
    public static String stringPointLength(String str, int num) {
        if (str.contains(".")) {
            List<String> list = StringUtil.stringToList(str, "\\.");
            int pointIndex = str.indexOf(".");
            int length = str.length() - 1 - pointIndex;
            if (length > num) {
                return list.get(0) + str.substring(pointIndex, pointIndex + num + 1);
            }

        }
        return str;
    }

    /**
     * 字符串分割成集合
     *
     * @param str
     * @param separator
     * @return
     */
    public static List<String> stringToList(String str, String separator) {
        if (null == str || null == separator || str.isEmpty()) {
            return Collections.emptyList();
        }
        return Arrays.stream(str.split(separator)).collect(Collectors.toList());
    }

    /**
     * 驼峰转下划线
     *
     * @param str
     * @return
     */
    public static String camelToUnderline(String str) {
        if (isBlank(str)) {
            return str;
        }

        Matcher matcher = camelPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     *
     * @param str
     * @return
     */
    public static String underlineToCamel(String str) {
        if (isBlank(str)) {
            return str;
        }

        str = str.toLowerCase();
        Matcher matcher = underlinePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static String integerNumberToString(Integer number) {
        if (number < 10) {
            return "1000000" + number;
        } else if (number < 100) {
            return "100000" + number;
        } else if (number < 1000) {
            return "10000" + number;
        } else if (number < 10000) {
            return "1000" + number;
        }
        return "100" + number;
    }

    public static String ObjectToString(Object obj) {
        if (obj == null) {
            return null;
        }
        return obj.toString();
    }


    public static String getSalt() {
        return getRandomChar(18);
    }

    public static String getRandomChar(int length) {
        char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(36)]);
        }
        return buffer.toString();
    }


}
